<template>
    <!--<div>
        <h1>修改！</h1>
    </div>-->
    <el-form style="width: 60%" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="ID" >
            <!--readonly 表示只读，不能修改-->
            <el-input v-model="ruleForm.sno" readonly></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="sname">
            <el-input v-model="ruleForm.sname"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="ssex">
            <el-input v-model="ruleForm.ssex"></el-input>
        </el-form-item>
        <el-form-item label="系别" prop="sdept">
            <el-input v-model="ruleForm.sdept"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="sage">
            <el-input v-model="ruleForm.sage"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即修改</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
            <!--                <el-button @click="test1()">测试</el-button>-->
        </el-form-item>
    </el-form>
</template>

<script>
    export default {
        data() {
            return {
                ruleForm: {
                    sno: '',
                    sname: '',
                    ssex: '',
                    sdept: '',
                    sage: '',
                },
                rules: {
                    sname: [
                        { required: true, message: '请输入名字', trigger: 'blur' },
                        { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' }
                    ],
                    ssex: [
                        { required: true, message: '请输入性别', trigger: 'blur' }
                    ],
                    sdept: [
                        { required: true, message: '请输入系别', trigger: 'blur' }
                    ],
                    sage: [
                        { required: true, message: '请输入年龄', trigger: 'blur' }
                    ],
                    /*type: [
                        { type: 'array', required: true, message: '请至少选择一个活动性质', trigger: 'change' }
                    ],
                    resource: [
                        { required: true, message: '请选择活动资源', trigger: 'change' }
                    ],
                    desc: [
                        { required: true, message: '请填写活动形式', trigger: 'blur' }
                    ]*/
                }
            };
        },
        methods: {
            /*test1(){
                console.log(this.ruleForm)
            },*/
            submitForm(formName) {
                const _this=this
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        // alert('submit!');
                        console.log(_this.ruleForm)
                        axios.post('http://localhost:8181/update',_this.ruleForm)
                            .then(function (response) {
                                /*console.log(response)*/
                                if (response.data=='success'){
                                    /*弹窗一*/
                                    /*_this.$message({
                                        message: '恭喜你，添加成功！',
                                        type: 'success'
                                    });*/
                                    /*弹窗二，并跳转*/
                                    _this.$alert(''+_this.ruleForm.sname+'修改成功！', '提示', {
                                        confirmButtonText: '确定',
                                        callback: action => {
                                            _this.$router.push('/findAll')
                                        }
                                    });
                                }
                            })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        created(){
            const _this=this
            // alert(this.$route.query.sno)
            axios.get('http://localhost:8181/findById/'+this.$route.query.sno)
                .then(function (response) {
                    // console.log(response.data())
                    _this.ruleForm=response.data
                })
        }
    }
</script>